package api;

import dao.User;
import dao.UserDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/logout")
public class LoginoutServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //创建一个session
        HttpSession session=req.getSession(false);
        //如果session为空，定向跳转到登录页面
        if(session==null)
        {
            resp.sendRedirect("login.html");
            return;
        }
        //如果是登陆成功，就会储存一个UERS
        User user=(User) session.getAttribute("user");
        UserDao userDao=new UserDao();
        userDao.deleteUserById(user.getUserId());
        //删除
        session.removeAttribute("user");

        resp.sendRedirect("login.html");
        //定向跳转到登陆页面
    }
}
